package com.laikan.reader.bookfriend.adapter.impl;

import android.content.Context;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.laikan.library.components.dialog.LoadingDialog;
import com.laikan.library.httpvolley.helper.RequestListener;
import com.laikan.library.httpvolley.helper.VolleyErrorHandler;
import com.laikan.library.httpvolley.response.HttpResponse;
import com.laikan.library.managers.ToastMgr;
import com.laikan.reader.R;
import com.laikan.reader.bookfriend.adapter.BaseListAdapter;
import com.laikan.reader.bookfriend.entity.AttentionBean;
import com.laikan.reader.bookfriend.entity.ItemFunsBean;
import com.laikan.reader.bookfriend.fragment.impl.FunsFragment;
import com.laikan.reader.bookfriend.holder.LViewHolder;
import com.laikan.reader.bookfriend.mactivity.AutherActivity;
import com.laikan.reader.bookfriend.requester.AutherRequester;
import com.laikan.reader.bookfriend.utils.Constant;
import com.laikan.reader.bookfriend.utils.ImageUtils;
import com.socks.library.KLog;

/**
 * Created by lingyt on 2016/11/15.
 * 支持加载dapter
 */
public class FunsAdapter extends BaseListAdapter<ItemFunsBean>{


    private Button mBt_attention;
    private TextView mTvTitle,mTvDes;
    private SimpleDraweeView mIcon;
    private LoadingDialog mLoadingDialog;
    private FunsFragment mFunsFragment;

    public FunsAdapter(Context context, int layoutId, FunsFragment funsFragment) {
        super(context, layoutId);
        mFunsFragment = funsFragment;
    }

    @Override
    protected void convert(final LViewHolder holder, final ItemFunsBean bean) {

        initView(holder);

        setListener(holder, bean);

        refreshView(bean);

    }

    private void initView(LViewHolder holder) {
        mTvTitle = holder.getView(R.id.item_tv_title);
        mBt_attention = holder.getView(R.id.item_bt);
        mIcon = holder.getView(R.id.item_iv_icon);
        mTvDes = holder.getView(R.id.item_tv_des);

        mLoadingDialog = new LoadingDialog(mContext);
    }

    private void refreshView(ItemFunsBean bean) {

        refreshAttenBtnStatus(mBt_attention,bean.isAttention);//初始化按钮的状态
        mTvTitle.setText(bean.funsName);//初始化标题
        mIcon.setImageURI(Uri.parse(bean.funsPic));
        mTvDes.setText(bean.content);
    }

    private void setListener(LViewHolder holder, final ItemFunsBean bean) {
        mBt_attention.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//                if(bean.isAttention){
//                    sendDelAttentionRequest(String.valueOf(bean.funsId));
//                }else{
//                    sendAddAttentionRequest(String.valueOf(bean.funsId));
//                }
                  currClickButton = (Button) v;

                  if (currClickButton.getText().equals("关注")){
                      sendAddAttentionRequest(bean.funsId);
                  }else if (currClickButton.getText().equals("已关注")){
                      sendDelAttentionRequest(bean.funsId);
                  }

            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击跳转到粉丝主页
                ImageUtils.startActivity(AutherActivity.class, Constant.USER_ID,bean.funsId);
            }
        });
    }


    private Button currClickButton;

    public static final int FLAG_ADD_ATTENTION = 1;
    public static final int FLAG_DEL_ATTENTION = 2;
    public static final int FLAG_ATTENTION_NONE = -1;
    public static int currFlag = 0;

    private void sendAddAttentionRequest(String objectId) {
        currFlag = FLAG_ADD_ATTENTION;
        AutherRequester.requestAddAttention(objectId,mHandler);
    }

    private void sendDelAttentionRequest(String objectId) {
        currFlag = FLAG_DEL_ATTENTION;
        AutherRequester.requestDelAttention(objectId,mHandler);
    }

    public RequestListener mHandler = new RequestListener() {
        @Override
        public void onPreExecute() {
            mFunsFragment.showDialog();
        }

        @Override
        public void onSuccess(HttpResponse httpResponse) {
            Gson gson = new Gson();
            AttentionBean bean = gson.fromJson(httpResponse.responseContent,AttentionBean.class);
            if (currFlag == FLAG_ADD_ATTENTION){
                onAddAttentionSuccess(bean);
            }else if (currFlag == FLAG_DEL_ATTENTION){
                onDelAttentionSuccess(bean);
            }
        }

        @Override
        public void onError(VolleyErrorHandler errorHandler) {
            if (currFlag == FLAG_ADD_ATTENTION){
                onAddAttentionError();
            }else if (currFlag == FLAG_DEL_ATTENTION){
                onDelAttentionError();
            }
        }

        @Override
        public void onFinish() {
            onAttentionFinish();
            mFunsFragment.dismissDialog();
        }
    };


    private void onDelAttentionError() {
        ToastMgr.showShortToast("取消失败");
    }

    private void onDelAttentionSuccess(AttentionBean bean) {
        if (bean!=null && bean.result.equals("1")){
            ToastMgr.showShortToast("取消成功");
            refreshAttenBtnStatus(currClickButton, false);
        }else{
            onDelAttentionError();
        }
    }

    private void onAddAttentionError() {
        ToastMgr.showShortToast("关注失败");
    }

    private void onAddAttentionSuccess(AttentionBean bean) {
        if (bean!=null && bean.result.equals("1")){
            ToastMgr.showShortToast("关注成功");
            refreshAttenBtnStatus(currClickButton, true);
        }else{
            onAddAttentionError();
        }
    }

    private void onAttentionFinish() {
        currFlag = FLAG_ATTENTION_NONE;
        currClickButton = null;
    }

    private void refreshAttenBtnStatus(Button clickBtn, boolean isAttention) {
        if (isAttention){
            clickBtn.setTextColor(ImageUtils.getContext().getResources().getColor(R.color.C_999999));//灰色
            clickBtn.setBackgroundResource(R.drawable.focus_shape_gray);
            clickBtn.setText("已关注");
            KLog.w("---------");
        }else{
            clickBtn.setTextColor(ImageUtils.getContext().getResources().getColor(R.color.C_66ccdd));
            clickBtn.setBackgroundResource(R.drawable.focus_shape_blue);
            clickBtn.setText("关注");
        }
    }

}
